package DAO;

import java.sql.Date;
import java.util.List;
import java.util.Vector;

import hibernate.AssetInfo;
import hibernateUtil.HibernateUtil;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ZCDJ {
	
	int totalByQuery = 0; //查询的资产信息的总记录
	
	/*
	 * save资产信息
	 */
    public void addAsset(String parameter[]){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		AssetInfo assetInfo = new AssetInfo();
		
/*		Date produceTime = java.sql.Date.valueOf(parameter[13]);
		Date startUseTime = java.sql.Date.valueOf(parameter[17]);
		double money = Double.parseDouble(parameter[14]);
		double buildCharge = Double.parseDouble(parameter[16]);
		double depreciation = Double.parseDouble(parameter[8]);
		int usedYears = Integer.parseInt(parameter[18]);
		int lifeYears = Integer.parseInt(parameter[19]);*/
		
		assetInfo.setAssetNo(parameter[0]);
		assetInfo.setAssetName(parameter[1]);
		assetInfo.setAssetBarcode(parameter[2]);
		assetInfo.setAssetState(parameter[3]);
		assetInfo.setBelongTo(parameter[4]);
		assetInfo.setComment(parameter[5]);
		assetInfo.setSku(parameter[6]);
		assetInfo.setDeviceType(parameter[7]);
		
		session.save(assetInfo);
		trans.commit();
    }
    
    @SuppressWarnings("unchecked")
    /*
     * 获取资产编号
     * 资产编号以F+当前日期+0001自增
     */
	public String getAssetNo()
    {
    	String result = "";
    	
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		List list = session.createQuery("select assetNo from AssetInfo order by assetNo desc").list();
		if(list.size() == 0)
		{
			result = "0001";
		}
		else
		{
			String assetNo = list.get(0).toString();
			String number = assetNo.substring(9, assetNo.length());
			int autoAdd = Integer.parseInt(number);
			autoAdd++;
			result = String.valueOf(autoAdd);
		}
		
		trans.commit();
		
		return result;
    }
    
    /*
     * 得到资产信息的总记录
     */
    public int getTotal()
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		int countTotal = (session.createQuery("from AssetInfo").list()).size(); //资产信息的总数;
		
		trans.commit();
		
		return countTotal;
    }
    
    /*
     * 查询的资产信息的总记录
     * 参数 -- 
     */
    public int getTotalByQuery()
    {
    	return totalByQuery;
    }
    
    @SuppressWarnings("unchecked")    
    /*
     * 通过参数得到资产的所有信息
     * 参数 start ， limit
     */
	public List getAllAsset(int start , int limit)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();

		Query query = session.createQuery("from AssetInfo");
		query.setFirstResult(start);
		query.setMaxResults(limit);
		List l = query.list();
		
		trans.commit();
		
		return l;
    }
    
    @SuppressWarnings("unchecked")
    /*
     * 通过查询条件得到符合条件的资产信息
     * 参数 --- assetNo ,assetName ,assetBarcode , assetState , BelongTo,Comment
     */
    public List getAssetByQuery(int start,int limit,String assetNo,String assetName,String assetBarcode,
    							String assetState,String belongTo,String comment,String sku,String deviceType)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		
		String str = "from AssetInfo where ";
		
		if(!"".equals(assetNo))
		{
			str = str + "assetNo=:assetNo ";
		}
		if(!"".equals(assetName))
		{
			if(!"".equals(assetNo))
			{
				str = str + "and assetName=:assetName ";
			}
			else
			{
				str = str + "assetName=:assetName ";
			}
		}
		if(!"".equals(assetBarcode))
		{
			if(!"".equals(assetNo) || !"".equals(assetName))
			{
				str = str + "and assetBarcode=:assetBarcode ";
			}
			else
			{
				str = str + "assetBarcode=:assetBarcode ";
			}
		}
		if(!"".equals(assetState))
		{
			if(!"".equals(assetNo) || !"".equals(assetName) || !"".equals(assetBarcode))
			{
				str = str + "and assetState=:assetState ";
			}
			else
			{
				str = str + "assetState=:assetState ";
			}
		}
		if(!"".equals(belongTo))
		{
			if(!"".equals(assetNo) || !"".equals(assetName) || !"".equals(assetBarcode) || !"".equals(assetState))
			{
				str = str + "and belongTo=:belongTo ";
			}	
			else
			{
				str = str + "belongTo=:belongTo ";
			}			
		}
		if(!"".equals(comment))
		{
			if(!"".equals(assetNo) || !"".equals(assetName) || !"".equals(assetBarcode) || !"".equals(assetState) || !"".equals(belongTo))
			{
				str = str + "and comment=:comment ";
			}	
			else
			{
				str = str + "comment=:comment ";
			}			
		}
		
		if(!"".equals(sku))
		{
			if(!"".equals(assetNo) || !"".equals(assetName) || !"".equals(assetBarcode) || !"".equals(assetState) || !"".equals(belongTo)|| !"".equals(comment))
			{
				str = str + "and sku=:sku ";
			}	
			else
			{
				str = str + "sku =:sku ";
			}			
		}
		
		if(!"".equals(deviceType))
		{
			if(!"".equals(assetNo) || !"".equals(assetName) || !"".equals(assetBarcode) || !"".equals(assetState) || !"".equals(belongTo)|| !"".equals(comment) || !"".equals(sku))
			{
				str = str + "and deviceType=:deviceType ";
			}	
			else
			{
				str = str + "deviceType =:deviceType";
			}			
		}
		
		Query query = session.createQuery(str);    //查询数据库
		
		if(!"".equals(assetNo))
		{
			query.setString("assetNo", assetNo);
		}
		if(!"".equals(assetName))
		{
			query.setString("assetName", assetName);
		}
		if(!"".equals(assetBarcode))
		{
			query.setString("assetBarcode", assetBarcode);
		}
		if(!"".equals(assetState))
		{
			query.setString("assetState", assetState);
		}
		if(!"".equals(belongTo))
		{
			query.setString("belongTo", belongTo);
		}
		if(!"".equals(comment))
		{
			query.setString("comment", comment);
		}
		if(!"".equals(sku))
		{
			query.setString("sku", sku);
		}
		if(!"".equals(deviceType))
		{
			query.setString("deviceType", deviceType);
		}
		
		/*if(!"".equals(startUseTime))
		{
			Date startUseTime_1 = java.sql.Date.valueOf(startUseTime);
			query.setDate("startUseTime", startUseTime_1);
		}
		if(!"".equals(EndTime))
		{
			Date EndTime_1 = java.sql.Date.valueOf(EndTime);
			query.setDate("EndTime", EndTime_1);
		}		*/
		
		totalByQuery = query.list().size(); //通过查询条件，得到资产信息的记录总数
		
		query.setFirstResult(start);    //查询记录从这一条开始；
		query.setMaxResults(limit); 	  //查询记录限制条数；
		List l = query.list();

		trans.commit();
		
		return l;
    }
    
    /*
     * 通过资产编号(assetNo)更新资产信息
     * 参数 -- 除了资产状态和资产编号其他都可以更新
     */
    public void updataAsset(String asset[])
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
/*		Date produceTime = java.sql.Date.valueOf(asset[11]);
		Date startUseTime = java.sql.Date.valueOf(asset[15]);
		double money = Double.parseDouble(asset[12]);
		double buildCharge = Double.parseDouble(asset[14]);
		double depreciation = Double.parseDouble(asset[6]);
		int usedYears = Integer.parseInt(asset[16]);
		int lifeYears = Integer.parseInt(asset[17]); */

//		Query q = session.createQuery("update AssetInfo set name=:name,belongTo=:belongTo,measure=:measure,supplyUnit=:supplyUnit,principal=:principal,produceUnit=:produceUnit,depreciation=:depreciation,appertainEquipment=:appertainEquipment,equipNumber=:equipNumber,model=:model,type=:type,produceDate=:produceDate,money=:money,department=:department,buildCharge=:buildCharge,startUseTime=:startUseTime,usedYears=:usedYears,lifeYears=:lifeYears,remark=:remark where assetNo=:assetNo");
		Query q = session.createQuery("update AssetInfo set assetName=:name,assetBarcode=:barcode,assetState=:state,belongTo=:belongTo,comment=:comment,sku=:sku,deviceType=:deviceType where assetNo=:assetNo");
		q.setString("name",asset[1]);
		q.setString("barcode",asset[2]);
		q.setString("state",asset[3]);
		q.setString("belongTo",asset[4]);
		q.setString("comment",asset[5]);
		q.setString("assetNo",asset[0]);
		q.setString("sku", asset[6]);
		q.setString("deviceType", asset[7]);
/*		q.setDouble("depreciation",depreciation);
		q.setString("appertainEquipment",asset[7]);
		q.setString("equipNumber",asset[8]);
		q.setString("model",asset[9]);
		q.setString("type",asset[10]);
		q.setDate("produceDate",produceTime);
		q.setDouble("money",money);
		q.setString("department",asset[13]);
		q.setDouble("buildCharge",buildCharge);
		q.setDate("startUseTime",startUseTime);
		q.setInteger("usedYears",usedYears);
		q.setInteger("lifeYears",lifeYears);
		q.setString("remark",asset[18]);
		q.setString("assetNo",asset[19]);*/
		
		q.executeUpdate();
		
		trans.commit();		
    }
    
    public void updataBelongToAsset(String asset[])
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
/*		Date produceTime = java.sql.Date.valueOf(asset[11]);
		Date startUseTime = java.sql.Date.valueOf(asset[15]);
		double money = Double.parseDouble(asset[12]);
		double buildCharge = Double.parseDouble(asset[14]);
		double depreciation = Double.parseDouble(asset[6]);
		int usedYears = Integer.parseInt(asset[16]);
		int lifeYears = Integer.parseInt(asset[17]); */

//		Query q = session.createQuery("update AssetInfo set name=:name,belongTo=:belongTo,measure=:measure,supplyUnit=:supplyUnit,principal=:principal,produceUnit=:produceUnit,depreciation=:depreciation,appertainEquipment=:appertainEquipment,equipNumber=:equipNumber,model=:model,type=:type,produceDate=:produceDate,money=:money,department=:department,buildCharge=:buildCharge,startUseTime=:startUseTime,usedYears=:usedYears,lifeYears=:lifeYears,remark=:remark where assetNo=:assetNo");
		Query q = session.createQuery("update AssetInfo set belongTo=:belongTo where assetNo=:assetNo");
		q.setString("belongTo",asset[1]);
		q.setString("assetNo",asset[0]);
/*		q.setDouble("depreciation",depreciation);
		q.setString("appertainEquipment",asset[7]);
		q.setString("equipNumber",asset[8]);
		q.setString("model",asset[9]);
		q.setString("type",asset[10]);
		q.setDate("produceDate",produceTime);
		q.setDouble("money",money);
		q.setString("department",asset[13]);
		q.setDouble("buildCharge",buildCharge);
		q.setDate("startUseTime",startUseTime);
		q.setInteger("usedYears",usedYears);
		q.setInteger("lifeYears",lifeYears);
		q.setString("remark",asset[18]);
		q.setString("assetNo",asset[19]);*/
		
		q.executeUpdate();
		
		trans.commit();		
    }
    
    
    public void changeAssetState(String assetNo,String state)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
/*		Date produceTime = java.sql.Date.valueOf(asset[11]);
		Date startUseTime = java.sql.Date.valueOf(asset[15]);
		double money = Double.parseDouble(asset[12]);
		double buildCharge = Double.parseDouble(asset[14]);
		double depreciation = Double.parseDouble(asset[6]);
		int usedYears = Integer.parseInt(asset[16]);
		int lifeYears = Integer.parseInt(asset[17]); */

//		Query q = session.createQuery("update AssetInfo set name=:name,belongTo=:belongTo,measure=:measure,supplyUnit=:supplyUnit,principal=:principal,produceUnit=:produceUnit,depreciation=:depreciation,appertainEquipment=:appertainEquipment,equipNumber=:equipNumber,model=:model,type=:type,produceDate=:produceDate,money=:money,department=:department,buildCharge=:buildCharge,startUseTime=:startUseTime,usedYears=:usedYears,lifeYears=:lifeYears,remark=:remark where assetNo=:assetNo");
		Query q = session.createQuery("update AssetInfo set assetState=:state where assetNo=:assetNo");
		q.setString("state",state);
		q.setString("assetNo", assetNo);
		q.executeUpdate();
		
		trans.commit();		
    }
    
    @SuppressWarnings("unchecked")
    /*
     * 通过资产编号（assetNo）删除资产信息
     */
	public void deleteAsset(Vector assetNo)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction(); 
		
		for(int i=0; i<assetNo.size(); i++ )
		{
			AssetInfo assetInfo = (AssetInfo)session.get(AssetInfo.class, assetNo.elementAt(i).toString());
			session.delete(assetInfo);
		}
		
		trans.commit();
    }
    
    
    
    /*
     * 通过查询条件得到需要导出的资产信息,EXcel
     * 参数 --- assetNo
     */
    public List saveAssetByQuery(Vector assetNo)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		
		String str = "from AssetInfo where ";
		
		if(!"".equals(assetNo.elementAt(0).toString()))
		{
			str = str + "assetNo=" + "'" + assetNo.elementAt(0).toString()+"' ";
		}
		for (int i=1;i<assetNo.size();++i)
		{
			if(!"".equals(assetNo.elementAt(i).toString()))
			{
				str = str + "or assetNo=" + "'" +assetNo.elementAt(i).toString()+"' ";
			}
		}
				
		Query query = session.createQuery(str);    //查询数据库
		
		totalByQuery = query.list().size(); //通过查询条件，得到资产信息的记录总数
		
//		query.setFirstResult(start);    //查询记录从这一条开始；
//		query.setMaxResults(limit); 	  //查询记录限制条数；
		List l = query.list();

		trans.commit();
		
		return l;
    }
    
}
